package com.mzy.datepackage.M2022.Apr.twEight;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2022/4/29 11:06
 * @Version 1.0
 */
public class Test {
    /**
     * @author mzy
     * @date 2022/4/28 10:43
     */
//    @Component
//    public class MysqlCrashedCheck {
//        private static volatile boolean serverCrashed = false;
//
////    @Autowired
////    BaseDaoFactory baseDaoFactory;
////
////    @Autowired
////    BaseDaoFactory daoFactory;
//
//        @Value("${spring.datasource.url}")
//        private String url;
//        @Value("${spring.datasource.username}")
//        private String username;
//        @Value("${spring.datasource.password}")
//        private String password;
//
//        // 如果要自己写，则不需要用druid连接池的方法。如果能在druid连接池找到对应的属性，则用druid中对应的属性。
//
//        //    @Autowired
////    MySqlValidConnectionChecker mySqlValidConnectionChecker;
//        @Scheduled(cron = "*/3 * * * * *")
//        public void queryMysqlStatus() {
//            ResultSet set = null;
//            Statement stat  = null;
//            Connection connection = null;
//            try {
//                System.out.println("启动");
////            DataSource dataSource = SpringBootUtils.getApplicationContext().getBean(DataSource.class);
//
//                System.out.println("测试");
////            		1.注册驱动
////            Class.forName("com.mysql.jdbc.Driver");
////		2.获取数据库连接
//                connection = DriverManager.getConnection(url, username, password);
////		3.获取执行sql语句的平台对象啊
//                stat = connection.createStatement();
////		4.执行sql语句
//                set = stat.executeQuery("select 1");
//
////            connection = dataSource.getConnection();
////            System.out.println("connection捕获");
////            boolean validConnection = isValidConnection(connection, "select 1", 5);
//
////            EntityManager em = baseDaoFactory.getEm();
////            Query nativeQuery = em.createNativeQuery("SELECT * FROM sys_boot_manage_file");
//                //查询对象
////            BaseDao<SysSecurityUser, Long> dao = daoFactory.getDao(SysSecurityUser.class);
////            List<SysSecurityUser> all = dao.findAll();
////            Query nativeQuery = en.createNativeQuery("SELECT * FROM sys_boot_manage_file;");
//
//                // System.out.println("druid检查:" + validConnection);
////            statement = connection.prepareStatement("select 1");
////            resultSet = statement.executeQuery();
//                System.out.println("捕获");
////            DruidUtils.release(resultSet,statement,connection);
//
//                serverCrashed = false;
//                System.out.println(serverCrashed);
//
//            }
//            catch (Exception e) {
//                System.out.println("异常");
//                serverCrashed = true;
//                System.out.println(serverCrashed());
//                e.printStackTrace();
//            }
//            finally {
//                if (set != null) {
//                    try {
//                        set.close();
//                    } catch (SQLException throwables) {
//                        throwables.printStackTrace();
//                    }
//                }
//                if (stat != null) {
//                    try {
//                        stat.close();
//                    } catch (SQLException throwables) {
//                        throwables.printStackTrace();
//                    }
//                }
////            if (connection != null) {
////                try {
////                    connection.close();
////                } catch (SQLException throwables) {
////                    throwables.printStackTrace();
////                }
////            }
//
//            }
//        }
//
//        public static boolean serverCrashed() {
//            return serverCrashed;
//        }
//
//    }
}
